const {src, dest, parallel, series, watch} = require('gulp')
const del = require('del')
const browserSync = require('browser-sync')

const loadPlugins = require('gulp-load-plugins')  // 是一个方法
const plugins = loadPlugins()
const bs = browserSync.create() // 自动创建一个服务器

// 安装了gulp-load-plugins之后，下面的就不需要了
const sass = require('gulp-sass')(require('sass'))
// const babel = require('gulp-babel')
// const swig = require('gulp-swig')
// const imagemin = require('gulp-imagemin')

const data = {
    menus: [
      {
        name: 'Home',
        icon: 'aperture',
        link: 'index.html'
      },
      {
        name: 'Features',
        link: 'features.html'
      },
      {
        name: 'About',
        link: 'about.html'
      },
      {
        name: 'Contact',
        link: '#',
        children: [
          {
            name: 'Twitter',
            link: 'https://twitter.com/w_zce'
          },
          {
            name: 'About',
            link: 'https://weibo.com/zceme'
          },
          {
            name: 'divider'
          },
          {
            name: 'About',
            link: 'https://github.com/zce'
          }
        ]
      }
    ],
    pkg: require('./package.json'),
    date: new Date()
}

const clean = () => {
    return del(['dist', 'temp'])
}

const style = () => {
    // { base: 'src'} 基准路径 如果不设置就会把所有文件都直接放到dist目录下，没有文件夹的约束
    // { outputStyle: 'expanded'}  完成展开的形式
    return src('src/assets/styles/*.scss', { base: 'src'})
        .pipe(sass({ outputStyle: 'expanded'}))
        .pipe(dest('temp'))
        .pipe(bs.reload({ stream: true }))  // 改变之后重新加载
}

// 引入sass后报错 The following tasks did not complete: style  Did you forget to signal async completion? 需要再安装npm i sass -d

const script = () => {
    return src('src/assets/scripts/*.js', { base: 'src' })
        .pipe(plugins.babel({ presets: ['@babel/preset-env']}))
        .pipe(dest('temp'))
        .pipe(bs.reload({ stream: true }))
}

const page = () => {
    return src('src/*.html', { base: 'src' })
        .pipe(plugins.swig({data}))
        .pipe(dest('temp'))
        .pipe(bs.reload({ stream: true }))
}

const images = () => {
    return src('src/assets/images/**', { base: 'src' })
        .pipe(plugins.imagemin())
        .pipe(dest('dist'))
}

const font = () => {
    return src('src/assets/fonts/**', { base: 'src' })
        .pipe(plugins.imagemin())
        .pipe(dest('dist'))
}

const extra = () => {
    return src('public/**', { base: 'public'})
    .pipe(dest('dist'))
}

const serve = () => {
    watch('src/assets/styles/*.scss', style)
    watch('src/assets/scripts/*.js', script)
    watch('src/*.html', page)
    watch([
        'src/assets/images/**',
        'src/assets/fonts/**',
        'public/**'
      ], bs.reload)

    bs.init({
        notify: false,
        port: 5000, // 端口
        open: true, // 自动打开浏览器
        // files: 'dist/**', // 如果数据改变页面会改变
        server: {
            baseDir: ['temp', 'src', 'public'],
            // routes先执行
            routes: {
                '/node_modules': 'node_modules'
            }
        }
    })
}

const useref = () => {
    return src('temp/*html', { base: 'temp'})
    .pipe(plugins.useref({ searchPath: ['temp', '.']}))
    .pipe(plugins.if(/\.js$/, plugins.uglify()))
    .pipe(plugins.if(/\.css$/, plugins.cleanCss()))
    .pipe(plugins.if(/\.html$/, plugins.htmlmin({
        collapseWhitespace: true,
        minifyCSS: true,
        minifyJS: true
    })))
    .pipe(dest('dist'))
}

const compile = parallel(style, script, page)

const build = series(
    clean, 
    parallel(
        series(compile, useref), 
        images, 
        font, 
        extra
        )
    )

const develop = series(compile, serve)


module.exports = {
    clean,
    build,
    develop
}